import pyttsx3

class TextToSpeech:
    def __init__(self):
        self.engine = pyttsx3.init()
        self.setup_voice()
        
    def setup_voice(self):
        # 设置中文语音
        voices = self.engine.getProperty('voices')
        for voice in voices:
            if "chinese" in voice.name.lower():
                self.engine.setProperty('voice', voice.id)
                break
        
    def speak(self, text):
        """朗读文本"""
        self.engine.say(text)
        self.engine.runAndWait() 